﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FishCtrl : MonoBehaviour
{
    public static List<FishCtrl> fsList = new List<FishCtrl>();
    public FishManager fm;
    public float speed1;
    public float speed2;
    public float speed;

    public static List<Vector3> touches;
    private float cd = 4;
    private float t;
    private Quaternion desRot;
    public Vector3 startPos;
    public Vector3 goPos;
    public Transform boneRoot;
    // Use this for initialization
    void Start()
    {
        speed = Random.Range(speed1, speed2);
        cd = Random.Range(0, 4);
        desRot = transform.rotation;
        startPos = transform.position;
        boneRoot = transform.GetChild(0).GetChild(0);
    }

    // Update is called once per frame
    void Update()
    {
        Debug.DrawLine(transform.position, goPos, Color.green);
        Move();
        //CHange();
        transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(goPos - transform.position), 10);
    }

    void Change()
    {
        t = 0;
        //desRot = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + Random.Range(-60f, 60f), transform.rotation.eulerAngles.z);
        goPos = startPos + new Vector3(Random.Range(-5f, 5), 0, Random.Range(-5f, 5));
        speed = Random.Range(speed1, speed2);
        //cd = Random.Range(0, 4);
    }

    private void Move()
    {
        transform.Translate(0,0,speed * Time.deltaTime);
        boneRoot.position = transform.position;
        if (Vector3.Distance(transform.position, goPos) < 0.6f || goPos == Vector3.zero)
        {
            Change();
        }
        //if (transform.position.x > fm.to.position.x || transform.position.x < fm.from.position.x || transform.position.z > fm.to.position.z || transform.position.z < fm.from.position.z)
        //{
        //    Quaternion qua = Quaternion.LookRotation(new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1)));
        //    if (Random.Range(0, 100) < 50)
        //    {
        //        //Vector3 pos = new Vector3(Random.Range(from.position.x, to.position.x), from.position.y, Random.Range(from.position.z, to.position.z));


        //        Vector3 fromPos = new Vector3(fm.from.position.x, fm.from.position.y, Random.Range(fm.from.position.z, fm.to.position.z));
        //        Vector3 toPos = new Vector3(fm.to.position.x, fm.to.position.y, Random.Range(fm.from.position.z, fm.to.position.z));
        //        transform.position = fromPos;
        //        transform.rotation = qua;// Quaternion.LookRotation(toPos - fromPos);
        //        speed = Random.Range(speed1, speed2);
        //        desRot = transform.rotation;
        //    }
        //    else
        //    {
        //        Vector3 fromPos = new Vector3(fm.to.position.x, fm.to.position.y, Random.Range(fm.from.position.z, fm.to.position.z));
        //        Vector3 toPos = new Vector3(fm.from.position.x, fm.from.position.y, Random.Range(fm.from.position.z, fm.to.position.z));
        //        transform.position = fromPos;
        //        transform.rotation = qua;// Quaternion.LookRotation(toPos - fromPos);
        //        speed = Random.Range(speed1, speed2);
        //        desRot = transform.rotation;
        //    }
        //}
    }

    public virtual void Check()
    {
        float minD = 999;
        Vector3 touch = Vector3.zero;
        for (int i = 0; i < touches.Count; i++)
        {
            float d = Vector3.Distance(transform.position, touches[i]);
            if (d < minD)
            {
                minD = d;
                touch = touches[i];
            }
        }
        if (minD < 2)
        {
            Vector3 dir = (transform.position - touch).normalized;
            //dir = new Vector3(dir.x, 0, dir.z).normalized;
            float d = 18 - minD;
            Do(dir * d);
        }

        Debug.DrawLine(transform.position, touch, Color.blue);
    }

    public void Do(Vector3 dirForce)
    {
        transform.rotation = Quaternion.LookRotation(dirForce);
        speed = dirForce.magnitude * Random.Range(speed1, speed2) / 6;
        goPos = transform.position + dirForce / 2f;
    }
}
